package com.icbc.common.fileupload;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import org.apache.commons.lang.time.DateUtils;

public class DateUtil {
	
	public static String toStr(Date date,String format){
		
		DateFormat df = new SimpleDateFormat(format);
		
		return df.format(date);
		
	}
	
	public static Date toDate(String dateStr){
		try {
			DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
			return df.parse(dateStr);
		} catch (Exception e) {
			e.printStackTrace();
			return new Date();
		}
	}
	
	public static int daysBetween(Date date1,Date date2){
		
		Calendar c = Calendar.getInstance();
		
		c.setTime(date1);
		long time1 = c.getTimeInMillis();
		
		c.setTime(date2);
		long time2 = c.getTimeInMillis();
		
		return Integer.parseInt(String.valueOf((time1-time2)/(1000*3600*24)));
	}
	
	public static void main(String[] args) {
		System.out.println(daysBetween(new Date(),toDate("2015-04-19")));
		
		System.out.println(DateUtils.addDays(new Date(), -Integer.parseInt("5")));
	}

}
